import commandMap from '.'
import ErrorType from '../../constants/errorType'
import { IGroupMessageEvent } from '../../interface/bot.interface'
import { getEpidemicData } from '../../request/other'
import { findArea, splitLine, wrapCommandFn } from '../../utils'
import { Next } from '../app'

/**
 * 获取本bot帮助信息
 */
export const getHelp = wrapCommandFn(async (e: IGroupMessageEvent, next: Next) => {
    e.group.sendMsg('支持的命令有：\n' + splitLine(Object.keys(commandMap).map((command) => '/' + command)))
    next()
})

/**
 * 获取指定地区疫情信息
 */
export const getEpidemicInfoOnArea = wrapCommandFn(async (e: IGroupMessageEvent, next: Next) => {
    try {
        const res = await getEpidemicData()
        const queryArea = e.args[0]
        const data = res.data
        if (!data.data) return
        // 迭代所有地区，找到指定地区
        const area = findArea(data.data.areaTree, queryArea)
        if (!area) return
        const info = splitLine([
            `地区：${area.name}`,
            `新增确诊：${area.today.confirm}`,
            `确诊：${area.total.confirm}`,
            `治愈：${area.today.heal}`,
            `死亡：${area.today.dead}`,
            `更新时间：${data.data.lastUpdateTime}`,
            `数据来自163`,
        ])
        e.group.sendMsg(info)
        next()
    } catch (error) {
        e.group.sendMsg(ErrorType.REQUEST_ERROR)
    }
})
